#include <iostream>
#include <cstdio>
using namespace std;
const int N = 5e4 + 5, mod = 4;
int n, m, cnt;
int d[N], pre[N];
int find(int x)
{
    if (x != pre[x])
    {
        int root = find(pre[x]);
        d[x] = (d[x] + d[pre[x]]) % mod;
        pre[x] = root;
    }
    return pre[x];
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        pre[i] = i;
    while (m--)
    {
        int t, x, y;
        scanf("%d %d %d", &t, &x, &y);
        if (x < 1 || x > n || y < 1 || y > n || t != 1 && x == y)
        {
            cnt++;
            continue;
        }
        int k = t - 1;
        int px = find(x), py = find(y);
        if (px == py && ((d[x] - d[y]) % mod + mod) % mod != k)
        {
            cnt++;
            continue;
        }
        if (px != py)
        {
            pre[px] = py;
            d[px] = ((k - d[x] + d[y]) % mod + mod) % mod;
        }
    }
    printf("%d\n", cnt);
    return 0;
}
